import './confirm.less';
import Confirm from './confirm';
import config from './config';
import Vue from 'vue';

const Modal = Vue.extend(Confirm);
const isServer = typeof window === 'undefined';
export default function (content, options) {
  if (isServer) return;
  if (!content && arguments.length < 2) return;
  let title = '';
  switch (arguments.length) {
    case 1:
      options = {};
      break;
    case 2:
      if (typeof options === 'string') {
        title = options;
        options = {};
      }
      break;
    default:
      title = arguments[1];
      options = arguments[2];
      break;
  }
  return new Promise((resolve) => {
    let modal = new Modal({
      el: document.createElement('div'),
      propsData: {
        ...config,
        title,
        content,
        ...options
      }
    });
    document.body.appendChild(modal.$el);
    modal.open = true;
    modal.$on('close', function (result) {
      setTimeout(() => {
        modal.$el && modal.$el.parentNode && modal.$el.parentNode.removeChild(modal.$el);
        modal.$destroy();
        modal = null;
      }, 500);
      return resolve(result);
    });
  });
}
